﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Willow.EFRepository;
using Willow.IRepository;
using Willow.IService;
using Willow.Model;
using Willow.Service;

namespace Willow.WebApplication
{
    public class AutofacConfig
    {
        public static void RegisterAutofac()
        {
            var builder = new ContainerBuilder();
            SetupResolveRules(builder);
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        private static void SetupResolveRules(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(Assembly.Load("Willow.EFRepository")).AsImplementedInterfaces().PropertiesAutowired().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(Assembly.Load("Willow.Service")).AsImplementedInterfaces().PropertiesAutowired().InstancePerHttpRequest();
            builder.RegisterType<WillowDBEntities>().As<DbContext>().InstancePerHttpRequest();
            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        }
    }
}